package com.renfei.multimediatask.ui.task10;

import android.os.Build;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.base.BaseActivity;
import com.renfei.multimediatask.ui.task10.decodec.BaseDecodec;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VideoPlayActivity extends BaseActivity {


    @BindView(R.id.startRecord)
    Button startRecord;
    @BindView(R.id.stopRecord)
    Button stopRecord;
    @BindView(R.id.ll_btn_layout)
    RelativeLayout llBtnLayout;
    private String targetFilePath;

    @BindView(R.id.surfaceView)
    SurfaceView surfaceView;


    private Surface surface;

    private BaseDecodec baseDecodec;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_play;
    }

    @Override
    protected void initListener() {
        targetFilePath = getParentFilePath() + "saveVideo.mp4";

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                surface = holder.getSurface();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @OnClick({R.id.startRecord, R.id.stopRecord})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.startRecord:
                if (null == baseDecodec) {
                    baseDecodec = new BaseDecodec(targetFilePath, surface);
                    baseDecodec.startPlay();
                }
                break;
            case R.id.stopRecord:
                if (null != baseDecodec) {
                    baseDecodec.stopPlay();
                }
                break;
        }
    }
}
